/**
 * Created by Administrator on 2016/7/22.
 */
var i=0;

var max_sortArray=new Array();
$(function(){
    GeneratingSpecificationsBtn(".row");

});
var AddBtn =function (obj) {
    $(".Specifications_"+obj+" i").on("click",function () {
        var index =  $().index();
        var _lable=$(this).parent().find('label')
        var InputColor = '<input type="text" class="inputColor"><i class="reduce_'+obj+'">-</i>';
        _lable.append(InputColor);
        ReduceBtn(obj);
    });
};

var ReduceBtn = function (obj) {
    $(".reduce_" + obj).on("click",function () {
       var this_Input = $(this).prev();
       $(this_Input).remove();
       $(this).remove();
    })
}

var RemoveRowBtn = function (obj) {
    $(".remove_row_"+obj).on("click",function () {
        var _self_row = $(this).parent();
        _self_row.remove();
    })
}

var GeneratingSpecificationsBtn = function (obj) {
    $(".GeneratingSpecificationsBtn").on("click",function () {
        var index =  $(obj).index();
        var _self = $(this).parent().parent();
        var ProductSpecifications=$(".ProductSpecifications").val();
        _self.append('<div class="row Specifications_'+index+'"><label>'+ProductSpecifications+'</label><i class="add_'+index+' i">+</i><i class="remove_row_'+index+' i">×</i></div>');
        RemoveRowBtn(index);
        AddBtn(index);
        FastCombinationBtn(obj);
    })
}
/**
 * 产品组合
 * @constructor
 */
var val_one = new Array();
var val_two = new Array();
var all;
var html ="";
var row_num;
var FastCombinationBtn = function (obj) {
    $(".FastCombinationBtn").on("click",function () {
        var rows = $(".row").index();
        for (var r = 0; r <rows; r++){
            var items = $(".Specifications_"+r).find("input");
            var item_val = new Array();
            if(all == undefined){
                all =  items.length;
            }else{
                all=all * items.length;
            }
            for (var i = 0; i < items.length; i++){
                var val_item = $(items[i]).val();
                item_val[i] = val_item;
            }
            val_one[r] = item_val;
            val_two[r] = 0;
        }
        html="";
        var a =toHtml(0);
        $("body").append(a);
        $("#rows").val($(".dataRow").size());
        debugger;
    })
};

var toHtml = function (num) {

    if(num == 0){
        html += "<div class='row ptb10 dataRow'>";
    }
    if(all+1 < val_two.length){
        return false;
    }
    html+="<input value='"+val_one[num][val_two[num]]+"' name='Col' class='inputColor'>";
    var  isAdd = true;
    if(num < val_two.length-1){
        for(var i = num; i<val_two.length-1; i++){
            if(val_two[i+1]<(val_one[i+1].length-1)){
                isAdd = false;
                break;
            }
        }
    }
    if(isAdd){
        if(val_two[num]<(val_one[num].length-1)){
            val_two[num] ++;
        }else{
            val_two[num] = 0;
        }
    }
    num++;
    if(num==val_one.length){
        html+="<input value='' class='inputColor' placeholder='请输入价格'><input value='' class='inputColor' placeholder='请输入库存'></div>";
        num = 0;
        all--;
    }
    if(all>0){
        toHtml(num);
    }
    return html;
}
